package com.vitabuybuy.tv.ui.mine.talent.live.viewmodel

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.PlayBackLiveListDto
import com.vitabuybuy.tv.inter.OnLiveManageItemClickListener
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.LiveDteilaDto
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.OrderRepository
import com.vitabuybuy.tv.network.TalentLiveRepository
import com.vitabuybuy.tv.ui.tencent.liveroom.repository.LiveRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class LiveManageViewModel : BaseViewModel() {


    val itemOnClickListener = object : OnLiveManageItemClickListener {
        override fun onViewLiveDataClick(item: PlayBackLiveListDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(1, obj = item))
        }

        override fun onViewPalyBackClick(item: PlayBackLiveListDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(2, obj = item))
        }
    }

    val liveList = ObservableArrayList<PlayBackLiveListDto>()

    val liveBinding: OnItemBind<PlayBackLiveListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: PlayBackLiveListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_live_replay_1)
                .bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    val playBackList = ObservableArrayList<PlayBackLiveListDto>()

    val playBackBinding: OnItemBind<PlayBackLiveListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: PlayBackLiveListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_live_replay_2)
                .bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    val liveSalesList = ObservableArrayList<String>()

    val liveSalesBinding: OnItemBind<String> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, _: String ->
            itemBinding.set(BR.itemBean, R.layout.item_live_sales_statistics)
                .bindExtra(BR.position, position)
//                .bindExtra(BR.listenner, itemOnClickListener)
        }


    fun getPlayBackList(show:Boolean,pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = TalentLiveRepository.getLiveList(show,pageNumber).await()
                if (pageNumber == 1) {
                    liveList.clear()
                }
                liveList.addAll(it)
                if (liveList.isEmpty()) {
                    statusView.set(LoadingType.SHOW_EMPTY)
                }else{
                    statusView.set(LoadingType.SHOW_CONTENT)
                }
                defUI.msgEvent.postValue(MessageModel(3, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val liveManageList = MutableLiveData<List<PlayBackLiveListDto>>()

    fun getPlayBackManageList(show:Boolean,pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                val it = TalentLiveRepository.getLiveList(show,pageNumber).await()
                liveManageList.value = it
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }


    fun deleteLivePlayBack(id: Int,position:Int) {
        rxHttpRequest {
            onRequest = {
                val it = TalentLiveRepository.deleteLivePlayBack(id).await()
                defUI.msgEvent.postValue(MessageModel(2, arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun isShowHome(show:Boolean,id:Int,position:Int) {
        rxHttpRequest {
            onRequest = {
                TalentLiveRepository.isShowHome(show,id).await()
                defUI.msgEvent.postValue(MessageModel(3, arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getData() {
        statusView.set(LoadingType.SHOW_CONTENT)
        liveSalesList.addAll(arrayListOf("", "", "", ""))
    }


    val liveDetils = MutableLiveData<LiveDteilaDto>()
    fun getLiveDetail(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                liveDetils.value = LiveRepository.getLiveDetail(liveId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
}
